# -*- coding: utf-8 -*-
"""
Created on Wed May 14 11:19:59 2025

@author: lenovo
"""

#  import 
import os 
from gsj_typhoon import extract_track_data
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np


#  paramlist
basepath = r'D:\met_data\ty_obs\storms' 
file_path = os.path.join(basepath,'storms_NH2024')


# 使用示例



track_id = 1
time, longitudes, latitudes, pmin, vmax = extract_track_data(file_path, track_id )
while  len(longitudes)!=0 :
    # 创建图形和坐标系
    plt.figure(figsize=(10, 8))
    ax = plt.axes(projection=ccrs.PlateCarree())
    
    # 设置地图范围
    margin = 3  # 边距
    ax.set_extent([min(longitudes) - margin, max(longitudes) + margin, 
                   min(latitudes) - margin, max(latitudes) + margin], 
                  crs=ccrs.PlateCarree())
    
    # 添加地图要素
    ax.add_feature(cfeature.LAND)
    ax.add_feature(cfeature.OCEAN)
    ax.add_feature(cfeature.COASTLINE, linewidth=0.5)
    ax.add_feature(cfeature.BORDERS, linestyle=':')
    ax.gridlines(draw_labels=True)
    
    # 绘制台风路径
    ax.plot(longitudes, latitudes, 'ro-', markersize=8, linewidth=2, 
            transform=ccrs.PlateCarree(), label='Typhoon Path')
    
    # 标记起点和终点
    ax.plot(longitudes[0], latitudes[0], 'go', markersize=10, 
            transform=ccrs.PlateCarree(), label='Start')
    ax.plot(longitudes[-1], latitudes[-1], 'bo', markersize=10, 
            transform=ccrs.PlateCarree(), label='End')
    
    # 添加标题和图例
    title = f'Typhoon Track {track_id}'
    plt.title(title ,  fontsize=15)
    plt.legend(loc='best')
    plt.savefig(os.path.join(basepath,title))
    plt.close()
    track_id+=1
    time, longitudes, latitudes, pmin, vmax = extract_track_data(file_path, track_id )
